package webdev.adapters;

import java.util.ArrayList;

import webdev.common.Common;
import webdev.database.SQLQuery;
import webdev.dto.BootKingDTO;
import webdev.dto.DiaDiemDTO;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import dev.danang.R;

public class BookingAdapter extends BaseAdapter {

	private LayoutInflater mInflater;
	private ArrayList<BootKingDTO> bookings;
	private ArrayList<DiaDiemDTO> diaDiems = new ArrayList<DiaDiemDTO>();
	
	public BookingAdapter(Context context) {
		mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		SQLQuery db = new SQLQuery(context);
		bookings = db.getAllBookingOfUser4Place(Common.currentUser.getmUserName());
//		bookings = db.getAllBooking();
		
		int size = bookings.size();
		DiaDiemDTO diaDiemDTO;
		for ( int i = 0; i < size; i++ ) {
			
			Log.i("TCT", bookings.get(i).getNameUser());
			
			diaDiemDTO = db.getDiaDiemById(bookings.get(i).getNamePlace());
			diaDiems.add(diaDiemDTO);
		}
		
//		addMoreItem();
	}
	
//	private void addMoreItem() {
//		BootKingDTO item1 = new BootKingDTO();
//		item1.setNamePlace("Khách sạn Gold Sea");
//		item1.setTimeArrive("01/04/2014");
//		item1.setTimeLeave("05/04/2014");
//		item1.setNote("Khách sạn này khá đẹp");
//		
//		BootKingDTO item2 = new BootKingDTO();
//		item2.setNamePlace("Khách sạn Thu Minh");
//		item2.setTimeArrive("01/03/2014");
//		item2.setTimeLeave("16/03/2014");
//		item2.setNote("Khách sạn này gần biển đẹp");
//		
//		bookings.add(item1);
//		bookings.add(item2);
//	}
	
	@Override
	public int getCount() {
		return bookings.size();
	}

	@Override
	public BootKingDTO getItem(int position) {
		return bookings.get(position);
	}

	public DiaDiemDTO getItemDiaDiem(int position) {
		return diaDiems.get(position);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup arg2) {
		ViewHolder viewHolder = null;
		BootKingDTO item = getItem(position);
		DiaDiemDTO itemDiaDiem = getItemDiaDiem(position);
		
		if (convertView == null) {
			convertView = mInflater.inflate(R.layout.item_list_bookings, null);
			viewHolder = new ViewHolder();
			viewHolder.txt_place = (TextView) convertView.findViewById(R.id.txt_place);
			viewHolder.txt_time_arrive = (TextView) convertView.findViewById(R.id.txt_time_arrive);
			viewHolder.txt_time_leave = (TextView) convertView.findViewById(R.id.txt_time_leave);
			viewHolder.txt_content = (TextView) convertView.findViewById(R.id.txt_content);
			
			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}
		
		viewHolder.txt_place.setText(itemDiaDiem.getTitle());
		viewHolder.txt_time_arrive.setText(item.getTimeArrive());
		viewHolder.txt_time_leave.setText(item.getTimeLeave());
		viewHolder.txt_content.setText(item.getNote());
		
		return convertView;
	}

	class ViewHolder {
		TextView txt_place;
		TextView txt_time_arrive;
		TextView txt_time_leave;
		TextView txt_content;
	}
}
